package com.etoak.common.jwt;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class JwtUtil {
    /**
     * 默认 256B = 32字符*8B
     */
    private static String SECRET_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    private static Key KEY = Keys.hmacShaKeyFor(SECRET_KEY.getBytes());

    private static long EXPIRE = 1000*60*60*12;
    /**
     * 创建jwt 自定义payload
     * @param claimsMap
     * @return
     */
    public  static String create(Map<String,Object> claimsMap){
        Date now = new Date();
        return Jwts.builder().signWith(KEY)//签名秘钥
                .setIssuedAt(now)//签发时间
                .setExpiration(new Date(now.getTime() + EXPIRE))
                .setClaims(claimsMap).compact();

    }

    public static Map<String,Object> parse(String token) {
        return Jwts.parserBuilder()
                .setSigningKey(KEY)
                .build()
                .parseClaimsJws(token)
                .getBody();
    }
}
